import { defineStore } from 'pinia';
import { messageFn } from '../../utils';

import {
    backRecordListApi,//冲红账单列表
    backCreateApi,//创建冲红记录
    backHandleApi,//冲红计算处理/冲减阶梯
    waterQtyRecordListApi,//冲减水量账单列表
    waterQtyCostCalcApi,//冲减水量费用计算
    waterQtyCostCreateApi//冲减水量记录创建
} from '/@/api/comprehensive/flushRed';

import { useLoading } from '/@/components/Loading';

const [openFullLoading, closeFullLoading] = useLoading({
    tip: '加载中...',
});


interface flushRedFace {
    backRecordList: []; //冲红账单列表结果
    backHandleList: [];//冲红计算处理/冲减阶梯结果
    backWaterQtyList: [];//冲减水量账单列表结果
    backWaterQtyCostCalc: [];//冲减水量费用计算结果
    total: number;
    pageInfo: object;
}

export const flushRedStore = defineStore({
    id: 'flushRedConfig-state',
    state: (): flushRedFace => ({
        backRecordList: [], //冲红账单列表结果
        backHandleList: [], //冲红计算处理/冲减阶梯结果
        backWaterQtyList: [],//冲减水量账单列表结果
        backWaterQtyCostCalc: [],//冲减水量费用计算结果
        total: 0,//总页数
        pageInfo: {
            //分页
            page_no: 1,
            page_size: 10,
        },
    }),
    getters: {

    },
    actions: {
        setTotal(total: number) {
            console.log('setTotal----', total);
            //总页数
            this.total = total;
        },
        setPageInfo(pageParams: object) {
            //设置页面数据
            this.pageInfo = pageParams;
        },
        //冲红账单列表结果
        setBackRecordList(recordList: []) {
            this.backRecordList = recordList;
        },
        //冲红计算处理/冲减阶梯结果
        setBackHandleList(handleList: []) {
            // console.log('handleList--123--', handleList);
            this.backHandleList = handleList;
        },
        //冲减水量账单列表结果
        setBackWaterQtyList(recordList: []) {
            this.backWaterQtyList = recordList;
        },
        //冲减水量费用计算结果
        setBackWaterQtyCostCalc(recordList: []) {
            this.backWaterQtyCostCalc = recordList;
        },

        /*
        * @description: bookListApi //冲红账单列表
        */
        async backRecordListAction(params: object) {
            openFullLoading();
            try {
                const backRecordList = await backRecordListApi(params);
                if (backRecordList.code === 0) {
                    this.setBackRecordList(backRecordList.data);
                    return true;
                } else {
                    messageFn('error', backRecordList.msg);
                    return false;
                }
            } finally {
                closeFullLoading();
            }

        },

        /* @description: bookListApi //冲红计算处理
        */
        async backHandleAction(params: object) {
            openFullLoading();
            try {
                const backHandle = await backHandleApi(params);
                if (backHandle.code === 0) {
                    const thisHandle = backHandle.data;
                    this.setBackHandleList(thisHandle)
                    return true;
                } else {
                    messageFn('error', backHandle.msg);
                    return false;
                }
            } finally {
                closeFullLoading();
            }

        },
        /**
* @description:  //创建冲红记录
*/
        async backCreateAction(params: object) {
            openFullLoading()
            try {
                const backCreate = await backCreateApi(params);
                if (backCreate.code === 0) {
                    messageFn('success', backCreate.msg);
                    return true;
                } else {
                    messageFn('error', backCreate.msg);
                    return false;
                }
            } finally {
                closeFullLoading()
            }
        },
        /*
                * @description: waterQtyRecordListApi //冲减水量账单列表
        */
        async waterQtyRecordListAction(params: object) {
            openFullLoading();
            try {
                const waterQtyRecordList = await waterQtyRecordListApi(params);
                if (waterQtyRecordList.code === 0) {
                    console.log('waterQtyRecordListAction----',waterQtyRecordList);
                    let newData:any = [];
                    newData.push(waterQtyRecordList.data);
                    this.setBackWaterQtyList(newData);
                    return true;
                } else {
                    messageFn('error', waterQtyRecordList.msg);
                    return false;
                }
            } finally {
                closeFullLoading();
            }

        },
        /*
              * @description: waterQtyCostCalcApi //冲减水量费用计算
      */
        async waterQtyCostCalcAction(params: object) {
            openFullLoading();
            try {
                const waterQtyCostCalc = await waterQtyCostCalcApi(params);
                if (waterQtyCostCalc.code === 0) {
                    return waterQtyCostCalc.data;
                } else {
                    messageFn('error', waterQtyCostCalc.msg);
                    return 'error';
                }
            } finally {
                closeFullLoading();
            }

        },
           /*
              * @description: waterQtyCostCalcApi //冲减水量费用计算
      */
           async waterQtyCostCreateAction(params: object) {
            openFullLoading();
            try {
                const waterQtyCostCreate = await waterQtyCostCreateApi(params);
                if (waterQtyCostCreate.code === 0) {
                    messageFn('success', waterQtyCostCreate.msg);
                    return true;
                } else {
                    messageFn('error', waterQtyCostCreate.msg);
                    return false;
                }
            } finally {
                closeFullLoading();
            }

        },

        
    }
});
